// 消费者
package com.xj.listener;

import com.xj.until.MailTestController;
import com.xj.until.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import java.util.Random;

// 异步消费
@Service
@RocketMQMessageListener(
        topic = "async",
        consumerGroup = "group-async"
)
@Slf4j
public class AsyncListener implements RocketMQListener<String> {
    @Autowired
    MailTestController mailTestController;
    @Autowired
    RedisUtils redisUtils;
    @Override
    public void onMessage(String s) {
        log.info("async消费者:  传过来的消息是 =={} ",s);
        Random rand = new Random();
        Integer num  = rand.nextInt(900000) + 100000; // 生成 100000 到 999999 之间的随机数
        redisUtils.set(s,num,300);
        System.out.println("邮件验证码"+num);
        try {
            System.out.println(mailTestController.sendHtmlMail1(num.toString(), s));
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}